# -*- coding: utf-8 -*-

'''自定义控件'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
import traceback
import  os


'''带点击事件的 图片label'''
class MyPicLabel(QWidget):

    doubleClick = pyqtSignal(str)
    delClick = pyqtSignal(str)

    def __init__(self, imgName, parent=None):
        super(MyPicLabel, self).__init__(parent=None)
        self.imgName = imgName
        try:
            self.initUI()
        except Exception as e:
            print(traceback.format_exc())


    def initUI(self):
        self.vlayout = QVBoxLayout()
        self.labelPic = QLabel()
        self.labelText = QLabel()
        self.pushButtonDel = QPushButton()

        self.labelPic.setFixedSize(200, 300)
        jpg = QPixmap(self.imgName).scaled(200, 300,Qt.KeepAspectRatio,Qt.SmoothTransformation) #等比例缩放
        self.labelPic.setPixmap(jpg)
        self.labelPic.setFrameShape(QFrame.Box)
        self.labelPic.setStyleSheet(
            'border-width: 2px;border-style: groove;border-color: #b8f5e1; background-color: #fff;')
        #self.labelPic.setScaledContents(True) 图片填满label

        self.labelText.setFixedSize(200, 20)
        self.labelText.setText(os.path.split(self.imgName)[1])
        self.labelText.setAlignment(Qt.AlignCenter)

        self.pushButtonDel.setFixedSize(200,20)
        self.pushButtonDel.setObjectName("btn_del")
        self.pushButtonDel.setText('删除')
        #self.pushButtonDel.setStyleSheet("background-color: red;")
        self.pushButtonDel.clicked.connect(self.delAction)

        self.vlayout.addWidget(self.labelPic)
        self.vlayout.addWidget(self.labelText)
        self.vlayout.addWidget(self.pushButtonDel)
        self.setLayout(self.vlayout)


    def delAction(self):
        alertInfo = QMessageBox.warning(self, '提示', '确定删除？', QMessageBox.Yes | QMessageBox.No)
        if alertInfo == QMessageBox.Yes:
            self.delClick.emit(self.imgName)


    def mouseDoubleClickEvent(self, *args, **kwargs):
        self.doubleClick.emit(self.imgName)


'''
自定义图片放大缩小
'''
class MyPictureZoom(QWidget):
    def __init__(self,imgName):
        super(MyPictureZoom, self).__init__()
        self.resize(500, 600)  # 设定窗口大小(根据自己显示图片的大小，可更改)
        self.setWindowTitle("图片查看器")  # 设定窗口名称
        self.imgName = imgName
        self.imgPixmap = QPixmap(imgName)  # 载入图片
        self.scaledImg = self.imgPixmap.scaled(self.size(),Qt.KeepAspectRatio,Qt.SmoothTransformation)  # 初始化缩放图
        self.singleOffset = QPoint(10, 10)  # 初始化偏移值

        self.isLeftPressed = bool(False)  # 图片被点住(鼠标左键)标志位
        self.isImgLabelArea = bool(True)

    '''重载绘图: 动态绘图'''
    def paintEvent(self, event):
        self.imgPainter = QPainter()  # 用于动态绘制图片
        self.imgFramePainter = QPainter()  # 用于动态绘制图片外线框
        self.imgPainter.begin(self)  # 无begin和end,则将一直循环更新
        self.imgPainter.drawPixmap(self.singleOffset, self.scaledImg)  # 从图像文件提取Pixmap并显示在指定位置
        self.imgFramePainter.setPen(QColor(168, 34, 3))  # 不设置则为默认黑色   # 设置绘图颜色/大小/样式
        self.imgFramePainter.drawRect(10, 10, 480, 480)  # 为图片绘外线狂(向外延展1)
        self.imgPainter.end()


    '''重载一下鼠标按下事件(单击)'''
    def mousePressEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:  # 左键按下
            print("鼠标左键单击")  # 响应测试语句
            self.isLeftPressed = True;  # 左键按下(图片被点住),置Ture
            self.preMousePosition = event.pos()  # 获取鼠标当前位置
        elif event.buttons() == QtCore.Qt.RightButton:  # 右键按下
            pass
        elif event.buttons() == QtCore.Qt.MidButton:  # 中键按下
            pass
        elif event.buttons() == QtCore.Qt.LeftButton | QtCore.Qt.RightButton:  # 左右键同时按下
            pass
        elif event.buttons() == QtCore.Qt.LeftButton | QtCore.Qt.MidButton:  # 左中键同时按下
            pass
        elif event.buttons() == QtCore.Qt.MidButton | QtCore.Qt.RightButton:  # 右中键同时按下
            pass
        elif event.buttons() == QtCore.Qt.LeftButton | QtCore.Qt.MidButton \
                | QtCore.Qt.RightButton:  # 左中右键同时按下
            pass


    '''重载一下滚轮滚动事件'''
    def wheelEvent(self, event):
        angle = event.angleDelta() / 8  # 返回QPoint对象，为滚轮转过的数值，单位为1/8度
        angleX = angle.x()  # 水平滚过的距离(此处用不上)
        angleY = angle.y()  # 竖直滚过的距离
        if angleY > 0:  # 滚轮上滚
            print("鼠标中键上滚")  # 响应测试语句
            self.scaledImg = self.imgPixmap.scaled(self.scaledImg.width() + 5,
                                                   self.scaledImg.height() + 5)
            newWidth = event.x() - (self.scaledImg.width() * (event.x() - self.singleOffset.x())) \
                       / (self.scaledImg.width() - 5)
            newHeight = event.y() - (self.scaledImg.height() * (event.y() - self.singleOffset.y())) \
                        / (self.scaledImg.height() - 5)
            self.singleOffset = QPoint(newWidth, newHeight)  # 更新偏移量
            self.repaint()  # 重绘
        else:  # 滚轮下滚
            print("鼠标中键下滚")  # 响应测试语句
            self.scaledImg = self.imgPixmap.scaled(self.scaledImg.width() - 5,
                                                   self.scaledImg.height() - 5)
            newWidth = event.x() - (self.scaledImg.width() * (event.x() - self.singleOffset.x())) \
                       / (self.scaledImg.width() + 5)
            newHeight = event.y() - (self.scaledImg.height() * (event.y() - self.singleOffset.y())) \
                        / (self.scaledImg.height() + 5)
            self.singleOffset = QPoint(newWidth, newHeight)  # 更新偏移量
            self.repaint()  # 重绘


    '''重载一下鼠标键公开事件'''
    def mouseReleaseEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:  # 左键释放
            self.isLeftPressed = False;  # 左键释放(图片被点住),置False
            print("鼠标左键松开")  # 响应测试语句
        elif event.button() == Qt.RightButton:  # 右键释放
            self.singleOffset = QPoint(0, 0)  # 置为初值
            self.scaledImg = self.imgPixmap.scaled(self.size())  # 置为初值
            self.repaint()  # 重绘
            print("鼠标右键松开")  # 响应测试语句


    '''重载一下鼠标移动事件'''
    def mouseMoveEvent(self, event):
        if self.isLeftPressed:  # 左键按下
            print("鼠标左键按下，移动鼠标")  # 响应测试语句
            self.endMousePosition = event.pos() - self.preMousePosition  # 鼠标当前位置-先前位置=单次偏移量
            self.singleOffset = self.singleOffset + self.endMousePosition  # 更新偏移量
            self.preMousePosition = event.pos()  # 更新当前鼠标在窗口上的位置，下次移动用
            self.repaint()


    #    '''重载一下鼠标双击事件'''
    #    def mouseDoubieCiickEvent(self, event):
    #        if event.buttons() == QtCore.Qt.LeftButton:                           # 左键按下
    #            self.setText ("双击鼠标左键的功能: 自己定义")
    #
    #
    #    '''重载一下鼠标进入控件事件'''
    #    def enterEvent(self, event):
    #
    #
    #    '''重载一下鼠标离开控件事件'''
    #    def leaveEvent(self, event):
    #

    #    '''重载一下鼠标进入控件事件'''
    #    def enterEvent(self, event):
    #
    #
    #    '''重载一下鼠标离开控件事件'''
    #    def leaveEvent(self, event):
    #